package BackTrackingPackage2;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class combinationSum3_ {

  List<Integer> path = new ArrayList<>();
  List<List<Integer>> result = new ArrayList<>();
  @Test
  public void combinationSum3Test(){
    combinationSum3(3,7);
    for (List<Integer> list : result) {
      System.out.println(list.toString());
    }
  }

  public List<List<Integer>> combinationSum3(int k,int n){
    backTracking(n,k,1);
    return result;
  }

  public void backTracking(int n, int k,int startIndex){
    if (getSum(path) == n && path.size() == k){
      result.add(new ArrayList<>(path));
      return;
    }
    for (int i = startIndex ; i <= 9 ; i++){
      path.add(i);
      backTracking(n,k,i+1);
      path.remove(path.size() - 1);
    }
  }
  public int getSum(List<Integer> path){
    int sum = 0;
    if (path.size() != 0 && path != null){
      for (Integer integer : path) {
        sum += integer;
      }
    }
    return sum;
  }
}
